package com.uniview.web.contorller;

import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uniview.base.BaseController;
import com.uniview.entity.po.RegionManager;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.ResultMessage;
import com.uniview.entity.vo.TableJsonVO;
import com.uniview.service.RegionManagerService;
import com.uniview.web.api.ServiceOrderApi;

@Controller
@RequestMapping(value = "/sys")
public class RegionManagerController extends BaseController {

	Logger logger = Logger.getLogger(RegionManagerController.class);

	@Autowired
	private RegionManagerService regionManagerService;

	@Autowired
	private ServiceOrderApi serviceOrderApi;


	@Value("#{configProperties['uniview.deptLevel']}")
	private int level;

	@RequestMapping(value = "/saveRegion")
	@ResponseBody
	public String saveRegion(RegionManager rm) {
		boolean result = true;
		String msg = "数据保存成功！";
		try {
			rm.setCreateTime(new Date());
			rm.setModifyTime(new Date());
			Integer Id = regionManagerService.saveRegion(rm);
			if (Id <= 0) {
				result = false;
				msg = "数据保存失败！";
			}
		} catch (Exception e) {
			result = false;
			msg = "数据保存失败！";
			logger.error("区域数据保存异常", e);
		}
		return getResultJsonString(msg, result);
	}

	@RequestMapping(value = "/updateRegion")
	@ResponseBody
	public String updateRegion(RegionManager rm) {
		boolean result = true;
		String msg = "数据修改成功！";
		try {
			rm.setModifyTime(new Date());
			result = regionManagerService.updateRegion(rm);
			if (!result) {
				msg = "数据修改失败！";
			}
		} catch (Exception e) {
			result = false;
			msg = "数据修改异常！";
			logger.error("区域数据修改异常", e);
		}
		return getResultJsonString(msg, result);
	}

	@RequestMapping(value = "/deleteRegion")
	@ResponseBody
	public String deleteRegion(HttpServletRequest request) {
		boolean result = true;
		String msg = "数据删除成功！";
		try {
			int Id = ServletRequestUtils.getIntParameter(request, "Id", -1);
			result = regionManagerService.deleteRegion(Id);
			if (!result) {
				msg = "数据删除失败！";
			}
		} catch (Exception e) {
			result = false;
			msg = "数据删除异常！";
			logger.error("区域数据删除异常", e);
		}
		return getResultJsonString(msg, result);
	}

	@RequestMapping(value = "/findRegionByPage")
	@ResponseBody
	public TableJsonVO findRegionByPage(HttpServletRequest request) {
		try {
			int page = ServletRequestUtils.getIntParameter(request, "page", 0);
			int limit = ServletRequestUtils.getIntParameter(request, "limit", 20);
			String regionName = ServletRequestUtils.getStringParameter(request, "regionName");
			Pager<RegionManager> pager = regionManagerService.findRegionByPage(regionName, limit, page);
			if (pager != null && pager.getPagerList() != null
					&& pager.getPagerList().size() > 0) {
//				return getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()), pager.getTotalRow());
				return getTableJsonVO(pager.getPagerList(), pager.getTotalRow());
			}
		} catch (Exception e) {

		}
//		return getTableJsonString("[]",0);
		return getTableJsonVO(new ArrayList<RegionManager>(), 0);
	}
	
	@RequestMapping(value = "/findRegionManager")
	@ResponseBody
	public ResultMessage findRegionManager(HttpServletRequest request){
		int id = ServletRequestUtils.getIntParameter(request, "Id", -1);
		try{
			RegionManager rm = regionManagerService.findRegionManager(id);
			if(rm !=null){
				return getResult("数据读取成功", true, rm);
			}
		}catch(Exception e){
			logger.error("数据读取异常", e);
		}
		return getResult("数据读取失败", false);
	}
	
	@RequestMapping(value = "/checkIsManager")
	@ResponseBody
	public ResultMessage checkIsManager(HttpServletRequest request){
		return serviceOrderApi.checkIsManager(request);
	}

}
